package com.primb.androidlibs.net.converter;

import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.net.entity.WXEntity;
import com.primb.androidlibs.net.entity.WXPayEntity;
import com.primb.androidlibs.net.exception.ResultException;
import com.primb.androidlibs.utils.JsonUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Converter;

import static com.tencent.bugly.crashreport.crash.c.e;


final class FastJsonResponseBodyConvert<T> implements Converter<ResponseBody, T> {

    private Type type;

    public FastJsonResponseBodyConvert(Type type) {
        this.type = type;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();
        T t;
        try {
            try {
                if (response.contains("access_token") && response.contains("openid") && response.contains("unionid")) {
                    t = JsonUtils.getObject(response, (Class<T>) type);
//                } else if (response.contains("prepayid") && response.contains("noncestr") && response.contains("timestamp") && !response.contains("dataReserve1")) {
//                    t = JsonUtils.getObject(response, (Class<T>) type);
                } else {
                    HttpResultEntity resultEntity = JsonUtils.getObject(response, HttpResultEntity.class);
                    if (resultEntity.getResultCode() == 1000) {
                        t = JsonUtils.getObject(response, (Class<T>) type);
                    } else {
                        ResultException resultException = new ResultException(resultEntity.getError());
                        throw resultException;
                    }
                }
            } catch (Exception e) {
                if (e instanceof ResultException) {
                    throw (ResultException) e;
                }
                throw new ResultException(e.getMessage());
            }
            return t;
        } finally {

        }
    }
}
